<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Java多线程 | 番园</title>
  <meta name="keywords" content="">
  <meta name="description" content="Java多线程 | 番园">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta property="og:type" content="website">
<meta property="og:title" content="番园">
<meta property="og:url" content="http://example.com/archives/index.html">
<meta property="og:site_name" content="番园">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="ShuYePL">
<meta name="twitter:card" content="summary">


<link rel="icon" href="/img/avatar1.jpg">

<link href="/css/style.css?v=1.1.0" rel="stylesheet">

<link href="/css/hl_theme/atom-dark.css?v=1.1.0" rel="stylesheet">

<link href="//cdn.jsdelivr.net/npm/animate.css@4.1.0/animate.min.css" rel="stylesheet">

<script src="//cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="/js/titleTip.js?v=1.1.0" ></script>

<script src="//cdn.jsdelivr.net/npm/highlightjs@9.16.2/highlight.pack.min.js"></script>
<script>
    hljs.initHighlightingOnLoad();
</script>

<script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js"></script>



<script src="//cdn.jsdelivr.net/npm/jquery.cookie@1.4.1/jquery.cookie.min.js" ></script>

<script src="/js/iconfont.js?v=1.1.0" ></script>

<meta name="generator" content="Hexo 5.4.0"></head>
<div style="display: none">
  <input class="theme_disqus_on" value="false">
  <input class="theme_preload_comment" value="">
  <input class="theme_blog_path" value="">
  <input id="theme_shortcut" value="false" />
  <input id="theme_highlight_on" value="true" />
  <input id="theme_code_copy" value="true" />
</div>



<body>
<aside class="nav">
    <div class="nav-left">
        <a href="/"
   class="avatar_target">
    <img class="avatar"
         src="/img/avatar1.jpg"/>
</a>
<div class="author">
    <span>ShuYePL</span>
</div>

<div class="icon">
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
            <a title="email"
               href="mailto:3548768596@qq.com"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-email"></use>
                    </svg>
                
            </a>
        
    
        
            <a title="qq"
               href="http://wpa.qq.com/msgrd?v=3&uin=3548768596&site=qq&menu=yes"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-qq"></use>
                    </svg>
                
            </a>
        
    
        
    
        
    
</div>




<ul>
    <li>
        <div class="all active" data-rel="All">All
            
                <small>(60)</small>
            
        </div>
    </li>
    
        
            
        
    
        
            
                <li>
                    <div data-rel="51单片机">
                        
                        51单片机
                        <small>(1)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="博客">
                        <i class="fold iconfont icon-right"></i>
                        
                        博客
                        <small>(1)</small>
                        
                    </div>
                    
                        <ul class="sub hide">
                            
                                <li>
                                    <div data-rel="博客<--->Hexo">
                                        
                                        Hexo
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                        </ul>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="博客建设">
                        
                        博客建设
                        <small>(3)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="单片机">
                        <i class="fold iconfont icon-right"></i>
                        
                        单片机
                        <small>(1)</small>
                        
                    </div>
                    
                        <ul class="sub hide">
                            
                                <li>
                                    <div data-rel="单片机<--->51单片机">
                                        
                                        51单片机
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                        </ul>
                    
                </li>
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
                <li>
                    <div data-rel="数据结构与算法">
                        
                        数据结构与算法
                        <small>(1)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
        
    
        
            
                <li>
                    <div data-rel="Git">
                        
                        Git
                        <small>(2)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
        
    
        
            
                <li>
                    <div data-rel="IDE">
                        <i class="fold iconfont icon-right"></i>
                        
                        IDE
                        <small>(1)</small>
                        
                    </div>
                    
                        <ul class="sub hide">
                            
                                <li>
                                    <div data-rel="IDE<--->IDEA">
                                        
                                        IDEA
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                        </ul>
                    
                </li>
            
        
    
        
            
        
    
        
            
                <li>
                    <div data-rel="Java力扣刷题笔记">
                        
                        Java力扣刷题笔记
                        <small>(2)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="Javase">
                        <i class="fold iconfont icon-right"></i>
                        
                        Javase
                        <small>(8)</small>
                        
                    </div>
                    
                        <ul class="sub hide">
                            
                                <li>
                                    <div data-rel="Javase<--->多线程">
                                        
                                        多线程
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="Javase<--->集合">
                                        
                                        集合
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="Javase<--->扩展运算符">
                                        
                                        扩展运算符
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="Javase<--->命令行问题">
                                        
                                        命令行问题
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="Javase<--->数组">
                                        
                                        数组
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="Javase<--->ThreadLocal">
                                        
                                        ThreadLocal
                                        
                                            <small>(1
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                        </ul>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="JavaWeb">
                        
                        JavaWeb
                        <small>(22)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="JDBC">
                        
                        JDBC
                        <small>(1)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="Linux">
                        
                        Linux
                        <small>(2)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="mybatis">
                        
                        mybatis
                        <small>(5)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="SpringMVC">
                        
                        SpringMVC
                        <small>(1)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="sql">
                        
                        sql
                        <small>(3)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
        
    
        
            
                <li>
                    <div data-rel="UML">
                        
                        UML
                        <small>(1)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="vscode使用">
                        
                        vscode使用
                        <small>(1)</small>
                        
                    </div>
                    
                </li>
            
        
    
        
            
                <li>
                    <div data-rel="Windows系统使用">
                        
                        Windows系统使用
                        <small>(1)</small>
                        
                    </div>
                    
                </li>
            
        
    
</ul>
<div class="left-bottom">
    <div class="menus">
        
            
            
            
    </div>
    <div>
        
        
    </div>
</div>
<input type="hidden" id="yelog_site_posts_number" value="60">

<div style="display: none">
    <span id="busuanzi_value_site_uv"></span>
    <span id="busuanzi_value_site_pv"></span>
</div>

    </div>
    <div class="nav-right">
        <div class="friends-area">
    <div class="friends-title">
        Links
        <i class="iconfont icon-left"></i>
    </div>
    <div class="friends-content">
        <ul>
            
            <li><a target="_blank" href="http://yelog.org/">叶落阁</a></li>
            
        </ul>
    </div>
</div>
        <div class="title-list">
    <div class="right-top">
        <div id="default-panel">
            <i class="iconfont icon-search" data-title="搜索"></i>
            <div class="right-title">All</div>
            <i class="iconfont icon-file-tree" data-title="切换到大纲视图"></i>
        </div>
        <div id="search-panel">
            <i class="iconfont icon-left" data-title="返回"></i>
            <input id="local-search-input" autocomplete="off"/>
            <label class="border-line" for="input"></label>
            <i class="iconfont icon-case-sensitive" data-title="大小写敏感"></i>
            <i class="iconfont icon-tag" data-title="标签"></i>
        </div>
        <div id="outline-panel" style="display: none">
            <div class="right-title">大纲</div>
            <i class="iconfont icon-list" data-title="切换到文章列表"></i>
        </div>
    </div>

    <div class="tags-list">
    <input id="tag-search" />
    <div class="tag-wrapper">
        
    </div>

</div>

    
    <nav id="title-list-nav">
        
        
        <a  class="All "
           href="/2023/04/12/%E7%BC%96%E7%A0%81/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="编码">编码</span>
            <span class="post-date" title="2023-04-12 09:40:51">2023/04/12</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2023/04/03/JWT/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="JWT">JWT</span>
            <span class="post-date" title="2023-04-03 22:33:20">2023/04/03</span>
        </a>
        
        
        <a  class="All mybatis "
           href="/2023/03/26/MyBatis%E4%B8%89%E5%A4%A7%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="MyBatis三大对象的生命周期">MyBatis三大对象的生命周期</span>
            <span class="post-date" title="2023-03-26 12:01:10">2023/03/26</span>
        </a>
        
        
        <a  class="All Javase ThreadLocal "
           href="/2023/03/26/ThreadLocal/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="ThreadLocal">ThreadLocal</span>
            <span class="post-date" title="2023-03-26 11:58:45">2023/03/26</span>
        </a>
        
        
        <a  class="All mybatis "
           href="/2023/03/25/MyBatis%E5%88%AB%E5%90%8D%E6%9C%BA%E5%88%B6/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="MyBatis别名机制">MyBatis别名机制</span>
            <span class="post-date" title="2023-03-25 15:07:07">2023/03/25</span>
        </a>
        
        
        <a  class="All mybatis "
           href="/2023/03/25/MyBatis%E4%B8%AD%E4%B8%A4%E7%A7%8D%E5%8D%A0%E4%BD%8D%E7%AC%A6%E7%9A%84%E5%8C%BA%E5%88%AB/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="MyBatis两种占位符的区别">MyBatis两种占位符的区别</span>
            <span class="post-date" title="2023-03-25 13:35:15">2023/03/25</span>
        </a>
        
        
        <a  class="All SpringMVC "
           href="/2023/03/24/springmvc%E7%9A%84%E4%B8%80%E4%B8%AA%E7%AE%80%E5%8D%95%E6%A8%A1%E6%9D%BF/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="springmvc的一个简单模板">springmvc的一个简单模板</span>
            <span class="post-date" title="2023-03-24 22:33:49">2023/03/24</span>
        </a>
        
        
        <a  class="All mybatis "
           href="/2023/03/24/MyBatis%E5%BC%95%E5%85%A5logback%E6%A1%86%E6%9E%B6/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="MyBatis引入logback框架">MyBatis引入logback框架</span>
            <span class="post-date" title="2023-03-24 14:20:51">2023/03/24</span>
        </a>
        
        
        <a  class="All Javase 集合 "
           href="/2023/03/21/Java%E9%9B%86%E5%90%88/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Java集合">Java集合</span>
            <span class="post-date" title="2023-03-21 19:14:53">2023/03/21</span>
        </a>
        
        
        <a  class="All sql "
           href="/2023/03/17/MySQL%E5%9F%BA%E6%9C%AC%E8%AF%AD%E5%8F%A5/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="MySQL基本语句">MySQL基本语句</span>
            <span class="post-date" title="2023-03-17 09:55:20">2023/03/17</span>
        </a>
        
        
        <a  class="All Javase 多线程 "
           href="/2023/03/16/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Java多线程">Java多线程</span>
            <span class="post-date" title="2023-03-16 19:14:28">2023/03/16</span>
        </a>
        
        
        <a  class="All sql "
           href="/2022/11/25/sql-%20%E6%8A%A5%E9%94%99%EF%BC%9APublic%20Key%20Retrieval%20is%20not%20allowed/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="sql- 报错：Public Key Retrieval is not allowed">sql- 报错：Public Key Retrieval is not allowed</span>
            <span class="post-date" title="2022-11-25 18:50:35">2022/11/25</span>
        </a>
        
        
        <a  class="All UML "
           href="/2022/11/25/UML-Rational%20Rose%E4%B8%8EUML%E7%AE%80%E5%8D%95%E7%AC%94%E8%AE%B0/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="UML-Rational Rose与UML简单笔记">UML-Rational Rose与UML简单笔记</span>
            <span class="post-date" title="2022-11-25 08:17:04">2022/11/25</span>
        </a>
        
        
        <a  class="All Javase 命令行问题 "
           href="/2022/11/18/Javase%E5%91%BD%E4%BB%A4%E8%A1%8C%E9%97%AE%E9%A2%98/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Javase命令行问题">Javase命令行问题</span>
            <span class="post-date" title="2022-11-18 10:31:15">2022/11/18</span>
        </a>
        
        
        <a  class="All Javase "
           href="/2022/11/14/Javase-Lambda%E8%A1%A8%E8%BE%BE%E5%BC%8F/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Javase-Lambda表达式">Javase-Lambda表达式</span>
            <span class="post-date" title="2022-11-14 15:19:33">2022/11/14</span>
        </a>
        
        
        <a  class="All Linux "
           href="/2022/11/06/Ubuntu-%E9%87%8D%E7%BD%AE%E5%AF%86%E7%A0%81/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Ubuntu-重置密码">Ubuntu-重置密码</span>
            <span class="post-date" title="2022-11-06 16:00:22">2022/11/06</span>
        </a>
        
        
        <a  class="All 51单片机 "
           href="/2022/11/06/51%E5%8D%95%E7%89%87%E6%9C%BA-%E5%85%89%E7%AB%8B%E6%96%B9%E6%8E%A7%E5%88%B6%E6%96%B9%E6%A1%88%E7%90%86%E8%A7%A3/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="51单片机-光立方控制方案理解">51单片机-光立方控制方案理解</span>
            <span class="post-date" title="2022-11-06 14:45:13">2022/11/06</span>
        </a>
        
        
        <a  class="All 单片机 51单片机 "
           href="/2022/11/05/51%E5%8D%95%E7%89%87%E6%9C%BA/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="51单片机">51单片机</span>
            <span class="post-date" title="2022-11-05 12:31:18">2022/11/05</span>
        </a>
        
        
        <a  class="All "
           href="/2022/10/02/%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%8D%95%E8%AF%8D/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="概念与单词">概念与单词</span>
            <span class="post-date" title="2022-10-02 20:57:00">2022/10/02</span>
        </a>
        
        
        <a  class="All Java力扣刷题笔记 "
           href="/2022/09/06/%E3%80%90Java%E5%8A%9B%E6%89%A3%E5%88%B7%E9%A2%98%E3%80%91%E9%A2%9820/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="【Java力扣刷题】题20">【Java力扣刷题】题20</span>
            <span class="post-date" title="2022-09-06 21:56:40">2022/09/06</span>
        </a>
        
        
        <a  class="All mybatis "
           href="/2022/09/04/Mybatis%E6%8A%A5%E9%94%99/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Mybatis报错">Mybatis报错</span>
            <span class="post-date" title="2022-09-04 15:20:06">2022/09/04</span>
        </a>
        
        
        <a  class="All "
           href="/2022/07/18/esp32%E4%B8%8A%E4%BC%A0%E6%8A%A5%E9%94%99/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="esp32上传报错">esp32上传报错</span>
            <span class="post-date" title="2022-07-18 21:22:45">2022/07/18</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/18/%E8%BF%87%E6%BB%A4%E5%99%A8/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="过滤器">过滤器</span>
            <span class="post-date" title="2022-07-18 21:15:12">2022/07/18</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/18/%E7%9B%91%E5%90%AC%E5%99%A8/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="监听器">监听器</span>
            <span class="post-date" title="2022-07-18 21:14:07">2022/07/18</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/15/%E5%AE%89%E5%85%A8%E9%80%80%E5%87%BA%E7%B3%BB%E7%BB%9F%E4%BD%BF%E7%94%A8%E9%87%8D%E5%AE%9A%E5%90%91%E5%92%8C%E8%BD%AC%E5%8F%91%E7%9A%84%E5%8C%BA%E5%88%AB/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="安全退出系统使用重定向和转发的区别">安全退出系统使用重定向和转发的区别</span>
            <span class="post-date" title="2022-07-15 17:23:39">2022/07/15</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/15/session%E5%92%8Ccookie%E8%81%94%E5%90%88%E4%BD%BF%E7%94%A8%E6%97%B6%E7%9A%84%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="session和cookie联合使用时的注意事项">session和cookie联合使用时的注意事项</span>
            <span class="post-date" title="2022-07-15 13:38:13">2022/07/15</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/12/JSTL%E6%A0%87%E7%AD%BE%E5%BA%93/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="JSTL标签库.md">JSTL标签库.md</span>
            <span class="post-date" title="2022-07-12 22:28:02">2022/07/12</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/12/EL%E8%A1%A8%E8%BE%BE%E5%BC%8F/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="EL表达式.md">EL表达式.md</span>
            <span class="post-date" title="2022-07-12 21:17:09">2022/07/12</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/10/cookie/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="cookie.md">cookie.md</span>
            <span class="post-date" title="2022-07-10 10:36:12">2022/07/10</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/10/session/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="session.md">session.md</span>
            <span class="post-date" title="2022-07-10 09:12:24">2022/07/10</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/07/06/JSP%E8%81%94%E5%90%88Servlet%E5%BC%80%E5%8F%91%E7%AE%80%E5%8D%95WEB%E7%A8%8B%E5%BA%8F/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="JSP联合Servlet开发简单WEB程序">JSP联合Servlet开发简单WEB程序</span>
            <span class="post-date" title="2022-07-06 23:37:55">2022/07/06</span>
        </a>
        
        
        <a  class="All sql "
           href="/2022/07/06/%E4%BD%BF%E7%94%A8source%E5%AF%BC%E5%85%A5sql%E8%84%9A%E6%9C%AC%E8%B7%AF%E5%BE%84%E9%94%99%E8%AF%AF%EF%BC%88Windows%E5%B9%B3%E5%8F%B0%EF%BC%89/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="使用source导入sql脚本路径错误（Windows平台）">使用source导入sql脚本路径错误（Windows平台）</span>
            <span class="post-date" title="2022-07-06 21:30:11">2022/07/06</span>
        </a>
        
        
        <a  class="All Git "
           href="/2022/07/04/%E4%BD%BF%E7%94%A8Gitee%E4%BB%93%E5%BA%93%E5%A4%87%E4%BB%BD%E6%96%87%E4%BB%B6/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="使用Gitee仓库备份文件">使用Gitee仓库备份文件</span>
            <span class="post-date" title="2022-07-04 19:54:08">2022/07/04</span>
        </a>
        
        
        <a  class="All Git "
           href="/2022/07/04/Git%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Git基本使用">Git基本使用</span>
            <span class="post-date" title="2022-07-04 19:47:12">2022/07/04</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/30/JSP/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="JSP">JSP</span>
            <span class="post-date" title="2022-06-30 10:45:40">2022/06/30</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/28/%E6%B3%A8%E8%A7%A3%E5%BC%8F%E5%BC%80%E5%8F%91/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="注解式开发">注解式开发</span>
            <span class="post-date" title="2022-06-28 23:23:47">2022/06/28</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/28/%E8%BD%AC%E5%8F%91%E5%92%8C%E9%87%8D%E5%AE%9A%E5%90%91/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="转发和重定向">转发和重定向</span>
            <span class="post-date" title="2022-06-28 21:23:21">2022/06/28</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/28/mysql%20%E7%9A%84%20ResultSet%20%E4%BD%BF%E7%94%A8%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="mysql 的 ResultSet 使用注意事项">mysql 的 ResultSet 使用注意事项</span>
            <span class="post-date" title="2022-06-28 20:29:42">2022/06/28</span>
        </a>
        
        
        <a  class="All Java力扣刷题笔记 "
           href="/2022/06/24/%E3%80%90Java%20%E5%8A%9B%E6%89%A3%E5%88%B7%E9%A2%98%E3%80%91%E9%A2%982/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="【Java 力扣刷题】题2">【Java 力扣刷题】题2</span>
            <span class="post-date" title="2022-06-24 19:56:00">2022/06/24</span>
        </a>
        
        
        <a  class="All 博客建设 "
           href="/2022/06/24/Steam++%20%E6%8C%82%E4%BA%86%E8%BF%98%E6%98%AF%E4%B8%8A%E4%B8%8D%E5%8E%BB%20Github%20%E7%9A%84%E8%A7%A3%E5%86%B3%E6%96%B9%E6%B3%95/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Steam++ 挂了还是上不去 Github 的解决方法">Steam++ 挂了还是上不去 Github 的解决方法</span>
            <span class="post-date" title="2022-06-24 09:28:00">2022/06/24</span>
        </a>
        
        
        <a  class="All 博客建设 "
           href="/2022/06/18/%E8%B4%AD%E4%B9%B0%E8%85%BE%E8%AE%AF%E4%BA%91%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%99%A8/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="购买腾讯云的服务器">购买腾讯云的服务器</span>
            <span class="post-date" title="2022-06-18 15:08:00">2022/06/18</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/08/HttpServletRequest/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="HttpServletRequest">HttpServletRequest</span>
            <span class="post-date" title="2022-06-08 22:34:00">2022/06/08</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/07/HttpServlet/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="HttpServlet">HttpServlet</span>
            <span class="post-date" title="2022-06-07 15:25:00">2022/06/07</span>
        </a>
        
        
        <a  class="All 博客建设 "
           href="/2022/06/06/XSHELL%20%E5%92%8C%20Xftp%20%E7%9A%84%E4%B8%8B%E8%BD%BD%E5%AE%89%E8%A3%85/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="XSHELL 和 Xftp 的下载安装">XSHELL 和 Xftp 的下载安装</span>
            <span class="post-date" title="2022-06-06 11:40:00">2022/06/06</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/04/ServletConfig%20%E6%98%AF%E4%BB%80%E4%B9%88/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="ServletConfig 是什么">ServletConfig 是什么</span>
            <span class="post-date" title="2022-06-04 21:52:00">2022/06/04</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/04/%E9%80%82%E9%85%8D%E5%99%A8%E6%A8%A1%E5%BC%8F%E6%94%B9%E9%80%A0Servlet/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="适配器模式改造Servlet">适配器模式改造Servlet</span>
            <span class="post-date" title="2022-06-04 12:58:00">2022/06/04</span>
        </a>
        
        
        <a  class="All IDE IDEA "
           href="/2022/06/03/IDEA%20%E7%9B%B8%E5%85%B3%E5%86%85%E5%AE%B9/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="IDEA 相关内容">IDEA 相关内容</span>
            <span class="post-date" title="2022-06-03 16:58:00">2022/06/03</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/06/03/Servlet%20%E5%AF%B9%E8%B1%A1%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Servlet 对象的生命周期">Servlet 对象的生命周期</span>
            <span class="post-date" title="2022-06-03 14:15:00">2022/06/03</span>
        </a>
        
        
        <a  class="All Linux "
           href="/2022/05/30/Linux%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Linux 学习笔记">Linux 学习笔记</span>
            <span class="post-date" title="2022-05-30 20:19:00">2022/05/30</span>
        </a>
        
        
        <a  class="All Windows系统使用 "
           href="/2022/05/24/Win11%20%E6%96%87%E4%BB%B6%E6%98%BE%E7%A4%BA%E5%90%8E%E7%BC%80%E5%90%8D/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Win11 文件显示后缀名">Win11 文件显示后缀名</span>
            <span class="post-date" title="2022-05-24 10:40:00">2022/05/24</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/05/07/%E4%BD%BF%E7%94%A8%20IDEA%20%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%20Tomcat%20%E9%A1%B9%E7%9B%AE/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="使用 IDEA 创建一个 Tomcat 项目">使用 IDEA 创建一个 Tomcat 项目</span>
            <span class="post-date" title="2022-05-07 22:37:00">2022/05/07</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/05/04/Servlet%20%E6%96%87%E4%BB%B6%E5%86%85%E5%AE%B9%E7%9A%84%E7%BB%93%E6%9E%84/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Servlet 文件内容的结构">Servlet 文件内容的结构</span>
            <span class="post-date" title="2022-05-04 12:44:00">2022/05/04</span>
        </a>
        
        
        <a  class="All JavaWeb "
           href="/2022/05/03/Tomcat%20%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Tomcat 环境配置">Tomcat 环境配置</span>
            <span class="post-date" title="2022-05-03 22:37:00">2022/05/03</span>
        </a>
        
        
        <a  class="All JDBC "
           href="/2022/05/03/JDBC%E7%9A%84%E4%BB%A3%E7%A0%81%E7%BB%93%E6%9E%84/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="JDBC的代码结构">JDBC的代码结构</span>
            <span class="post-date" title="2022-05-03 21:42:00">2022/05/03</span>
        </a>
        
        
        <a  class="All 数据结构与算法 "
           href="/2022/04/09/%E5%BF%AB%E9%80%9F%E6%8E%92%E5%BA%8F/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="快速排序">快速排序</span>
            <span class="post-date" title="2022-04-09 22:25:12">2022/04/09</span>
        </a>
        
        
        <a  class="All Javase 扩展运算符 "
           href="/2022/02/27/Java%E4%B8%AD%E7%9A%84%E6%89%A9%E5%B1%95%E8%BF%90%E7%AE%97%E7%AC%A6/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Java中的扩展运算符">Java中的扩展运算符</span>
            <span class="post-date" title="2022-02-27 11:24:50">2022/02/27</span>
        </a>
        
        
        <a  class="All vscode使用 "
           href="/2021/12/10/vscode%E4%B8%AD%E5%87%BA%E7%8E%B0Open%20a%20folder%20or%20workspace...%E6%8A%A5%E9%94%99/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="vscode中出现Open a folder or workspace...报错">vscode中出现Open a folder or workspace...报错</span>
            <span class="post-date" title="2021-12-10 00:23:35">2021/12/10</span>
        </a>
        
        
        <a  class="All Javase "
           href="/2021/11/21/Java%20main%E6%96%B9%E6%B3%95%E4%B8%AD%E5%8F%82%E6%95%B0String%5B%5D%20args%E7%9A%84%E7%94%A8%E6%B3%95/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Java main方法中参数String[] args的用法">Java main方法中参数String[] args的用法</span>
            <span class="post-date" title="2021-11-21 11:39:57">2021/11/21</span>
        </a>
        
        
        <a  class="All Javase 数组 "
           href="/2021/11/11/Java%E6%95%B0%E7%BB%84/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Java数组">Java数组</span>
            <span class="post-date" title="2021-11-11 16:54:26">2021/11/11</span>
        </a>
        
        
        <a  class="All 博客 Hexo "
           href="/2021/10/27/Hexo%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA%E7%AC%94%E8%AE%B0/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="Hexo博客搭建笔记">Hexo博客搭建笔记</span>
            <span class="post-date" title="2021-10-27 01:41:52">2021/10/27</span>
        </a>
        
        <div id="no-item-tips">

        </div>
    </nav>
    <div id="outline-list">
    </div>
</div>

    </div>
    <div class="hide-list">
        <div class="semicircle" data-title="切换全屏">
            <div class="brackets first"><</div>
            <div class="brackets">&gt;</div>
        </div>
    </div>
</aside>
<div id="post">
    <div class="pjax">
        <article id="post-Java多线程" class="article article-type-post" itemscope itemprop="blogPost">
    
        <h1 class="article-title">Java多线程</h1>
    
    <div class="article-meta">
        
        
        
        <span class="book">
            <i class="iconfont icon-category"></i>
            
            
            <a  data-rel="Javase">Javase</a> > 
            
            <a  data-rel="Javase&lt;---&gt;多线程">多线程</a>
            
        </span>
        
        
    </div>
    <div class="article-meta">
        
            Created At : <time class="date" title='Updated At: 2024-01-19 23:04:28'>2023-03-16 19:14</time>
        
    </div>
    <div class="article-meta">
        
        
        <span id="busuanzi_container_page_pv">
            Views 👀 :<span id="busuanzi_value_page_pv">
                <span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </span>
        </span>
        
        
    </div>
    
    <div class="toc-ref">
    
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%A4%9A%E7%BA%BF%E7%A8%8B"><span class="toc-text">多线程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B%E7%9A%84%E6%96%B9%E6%B3%95"><span class="toc-text">创建线程的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9E%E7%8E%B0Runnable%E6%8E%A5%E5%8F%A3-%EF%BC%88-%EF%BC%89"><span class="toc-text">实现Runnable接口 （*）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BB%A7%E6%89%BFThread%E7%B1%BB%E6%9C%AC%E8%BA%AB-%EF%BC%88-%EF%BC%89"><span class="toc-text">继承Thread类本身 （*）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%9A%E8%BF%87Callable%E5%92%8CFuture%E5%88%9B%E5%BB%BA%E7%BA%BF%E7%A8%8B"><span class="toc-text">通过Callable和Future创建线程</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%9A%84%E5%BB%BA%E8%AE%AE"><span class="toc-text">使用多线程的建议</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-text">线程的生命周期</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-text">线程的一些常用方法</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E5%81%9C%E6%AD%A2"><span class="toc-text">线程停止</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E4%BC%91%E7%9C%A0"><span class="toc-text">线程休眠</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E7%A4%BC%E8%AE%A9"><span class="toc-text">线程礼让</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E6%8F%92%E9%98%9F"><span class="toc-text">线程插队</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E7%8A%B6%E6%80%81%E6%A3%80%E6%B5%8B"><span class="toc-text">线程状态检测</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E4%BC%98%E5%85%88%E7%BA%A7"><span class="toc-text">线程优先级</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%88%E6%8A%A4%E7%BA%BF%E7%A8%8B"><span class="toc-text">守护线程</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%BF%E7%A8%8B%E5%90%8C%E6%AD%A5%E6%9C%BA%E5%88%B6"><span class="toc-text">线程同步机制</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#synchronized"><span class="toc-text">synchronized</span></a></li></ol></li></ol>
    
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
    .toc-level-3 i,
    .toc-level-3 ol {
        display: none !important;
    }
</style>
</div>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="多线程"><a href="#多线程" class="headerlink" title="多线程"></a>多线程</h1><p>一个程序对应于一个进程，一个进程可以对应多个线程。一个线程指的是进程中一个单一顺序的控制流，可以把线程当做是一个程序里面需要处理的各种分立任务，像是一个 Java 程序的执行就对应着一个进程，但是它在运行的时候至少包含两个线程，一个是 main 线程，一个是 gc 线程。一个进程包括由操作系统分配的内存空间，包含一个或多个线程。一个线程不能独立的存在，它必须是进程的一部分。一个进程一直运行，直到所有的非守护线程都结束运行后才能结束。</p>
<h1 id="创建线程的方法"><a href="#创建线程的方法" class="headerlink" title="创建线程的方法"></a>创建线程的方法</h1><h2 id="实现Runnable接口-（-）"><a href="#实现Runnable接口-（-）" class="headerlink" title="实现Runnable接口 （*）"></a>实现Runnable接口 （*）</h2><pre><code class="java">package com.shuyepl.demo;

public class demo &#123;
    public static void main(String[] args) &#123;
        Thread t2 = new Thread(new MyThread());
        t2.start();

        for (int i = 0; i &lt; 10; i++) &#123;
            System.out.println(&quot;main ==&gt; &quot; + i);
        &#125;
    &#125;
&#125;

class MyThread implements Runnable &#123;
    public void run()&#123;
        for (int i = 0; i &lt; 10; i++) &#123;
            System.out.println(&quot;MyThread ==&gt; &quot; + i);
        &#125;
    &#125;
&#125;
</code></pre>
<p>运行结果：</p>
<pre><code>main ==&gt; 0
main ==&gt; 1
main ==&gt; 2
main ==&gt; 3
main ==&gt; 4
main ==&gt; 5
MyThread ==&gt; 0
main ==&gt; 6
main ==&gt; 7
main ==&gt; 8
main ==&gt; 9
MyThread ==&gt; 1
MyThread ==&gt; 2
MyThread ==&gt; 3
MyThread ==&gt; 4
MyThread ==&gt; 5
MyThread ==&gt; 6
MyThread ==&gt; 7
MyThread ==&gt; 8
MyThread ==&gt; 9
</code></pre>
<h2 id="继承Thread类本身-（-）"><a href="#继承Thread类本身-（-）" class="headerlink" title="继承Thread类本身 （*）"></a>继承Thread类本身 （*）</h2><pre><code class="java">package com.shuyepl.demo;

public class demo &#123;
    public static void main(String[] args) &#123;
        MyThread t1 = new MyThread();
        t1.start();

        for (int i = 0; i &lt; 10; i++) &#123;
            System.out.println(&quot;main ==&gt; &quot; + i);
        &#125;
    &#125;
&#125;

class MyThread extends Thread &#123;
    public void run()&#123;
        for (int i = 0; i &lt; 10; i++) &#123;
            System.out.println(&quot;MyThread ==&gt; &quot; + i);
        &#125;
    &#125;
&#125;
</code></pre>
<p>运行结果：</p>
<pre><code>main ==&gt; 0
MyThread ==&gt; 0
MyThread ==&gt; 1
MyThread ==&gt; 2
MyThread ==&gt; 3
MyThread ==&gt; 4
MyThread ==&gt; 5
MyThread ==&gt; 6
MyThread ==&gt; 7
MyThread ==&gt; 8
main ==&gt; 1
main ==&gt; 2
MyThread ==&gt; 9
main ==&gt; 3
main ==&gt; 4
main ==&gt; 5
main ==&gt; 6
main ==&gt; 7
main ==&gt; 8
main ==&gt; 9
</code></pre>
<h2 id="通过Callable和Future创建线程"><a href="#通过Callable和Future创建线程" class="headerlink" title="通过Callable和Future创建线程"></a>通过Callable和Future创建线程</h2><h1 id="使用多线程的建议"><a href="#使用多线程的建议" class="headerlink" title="使用多线程的建议"></a>使用多线程的建议</h1><p>通过上面的例子可以看出来，使用继承 Thread 类来创建多线程的方式和使用实现 Runnable 接口来实现多线程的方式差不多，区别在于前者可以直接使用对象的 run 方法来进行多线程，而后者则需要将对象传入一个 Thread 对象中，再去调用 Thread 对象的 run 方法。</p>
<p>在使用的时候，考虑到 Java 里面只支持单继承，所以，采用实现 Runnable 接口的方式实现多线程是一个比较好的方式。</p>
<p>同时，使用 Runnable 实现多线程还有一个好处是，可以将同一个实现了 Runnable 接口的类对象丢进多个 Thread 对象中，实现多个线程。</p>
<p>下面是一个简单的例子，缺点是没有加上相应的线程控制，所以会出现一些不合预期的情况，比如从结果中可以看到，t3 和 t2 共同抢到了 1 号 gift ，而我们想的是每个 gift 只能给一个人，这样，就出现问题了。</p>
<pre><code class="java">public class Demo01 &#123;
    public static void main(String[] args)&#123;
        MyThread myThread = new MyThread();
        new Thread(myThread, &quot;t1&quot;).start();
        new Thread(myThread, &quot;t2&quot;).start();
        new Thread(myThread, &quot;t3&quot;).start();
    &#125;
&#125;

class MyThread implements Runnable &#123;
    public int total = 10;

    public void run () &#123;
        while (true) &#123;
            if (total &lt;= 0) &#123;
                break;
            &#125;
            System.out.println(Thread.currentThread().getName() + &quot; get &quot; + (10 - total) + &quot; gift &quot;);
            total--;
        &#125;
    &#125;
&#125;
</code></pre>
<p>运行结果：</p>
<pre><code>t3 get 0 gift 
t3 get 1 gift 
t1 get 0 gift 
t1 get 3 gift 
t2 get 0 gift 
t1 get 4 gift 
t1 get 6 gift 
t1 get 7 gift 
t1 get 8 gift 
t1 get 9 gift 
t3 get 2 gift 
t2 get 5 gift 
</code></pre>
<h1 id="线程的生命周期"><a href="#线程的生命周期" class="headerlink" title="线程的生命周期"></a>线程的生命周期</h1><p>一个线程的可能状态有五种： <strong>新建状态、就绪状态、运行状态、死亡状态、阻塞状态</strong></p>
<p><img src="/2023/03/16/Java%E5%A4%9A%E7%BA%BF%E7%A8%8B/202209211551097.png" alt="2022090012"></p>
<p>新建状态（创建状态）：当一个线程被 new 出来之后就是这个状态。</p>
<p>就绪状态：线程调用 start() 方法后等待 CPU 的调度；处于运行状态的线程释放 CPU 资源进入就绪状态，等待 CPU 的再次调度；阻塞状态解除之后进入就绪状态。</p>
<p>运行状态：就绪状态的线程获得 CPU 资源后进入运行状态。</p>
<p>处于运行状态的线程可以转变为以下三种状态：阻塞状态、死亡状态、就绪状态。</p>
<p>阻塞分为三种：等待阻塞、同步阻塞、其他阻塞</p>
<ul>
<li>等待阻塞：调用线程的wait方法会使线程进入等待阻塞</li>
<li>同步阻塞：获取同步锁失败时会进入同步阻塞</li>
<li>其他阻塞：通过线程的sleep方法或者join方法进入该状态，等该状态结束之后会进入就绪状态</li>
</ul>
<p>死亡状态：线程自然执行完毕或者外部干涉终止该线程，一旦进入死亡状态就不能再次启动。</p>
<h1 id="线程的一些常用方法"><a href="#线程的一些常用方法" class="headerlink" title="线程的一些常用方法"></a>线程的一些常用方法</h1><table>
<thead>
<tr>
<th align="center">方法</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">setPriority(int newPriority)</td>
<td align="center">更改线程的优先级</td>
</tr>
<tr>
<td align="center">static void sleep(long millis)</td>
<td align="center">让正在执行的线程休眠指定的毫秒数</td>
</tr>
<tr>
<td align="center">void join()</td>
<td align="center">插入该线程进行执行，等待该线程执行结束</td>
</tr>
<tr>
<td align="center">static void yield()</td>
<td align="center">暂停执行的线程对象，执行其他线程</td>
</tr>
<tr>
<td align="center">void interrupt()</td>
<td align="center">中断线程</td>
</tr>
<tr>
<td align="center">boolean isAlive()</td>
<td align="center">测试线程是否处于活动状态</td>
</tr>
<tr>
<td align="center">stop()</td>
<td align="center">停止线程的运行</td>
</tr>
</tbody></table>
<h2 id="线程停止"><a href="#线程停止" class="headerlink" title="线程停止"></a>线程停止</h2><p>上面写的那些能让线程停止的方法不太建议使用，建议让线程自动停止，就是说在程序 run 方法里面写上让线程结束退出（运行到结尾）的条件。 </p>
<p>退出线程的示例：</p>
<pre><code class="java">package com.shuyepl.threadState;

public class StopTest implements Runnable &#123;
    // 判断线程是否停止的标志位,true 不停止，false 停止
    private boolean flag = true;

    @Override
    public void run() &#123;
        int i = 0;
        while (flag) &#123;
            System.out.println(&quot;running......&quot; + i++);
        &#125;
    &#125;

    // 让线程停下来的一个stop方法，我自己写的
    public void stop() &#123;
        flag = false;
    &#125;

    public static void main(String[] args) &#123;
        // 创建并启动线程
        StopTest stopTest = new StopTest();
        new Thread(stopTest).start();

        // 满足条件时调用我们自己写的stop方法，让线程停下来
        for (int i = 0; i &lt; 1000; i++) &#123;
            System.out.println(&quot;main ==&gt; &quot; + i);
            if (i == 900) &#123;
                stopTest.stop();
            &#125;
        &#125;
    &#125;
&#125;
</code></pre>
<h2 id="线程休眠"><a href="#线程休眠" class="headerlink" title="线程休眠"></a>线程休眠</h2><p>sleep 执行的时候不会释放对象上面的锁。</p>
<p>巧妙利用 sleep 方法可以模拟网络延时，还有就是放大一些并发问题的发生性（就是在本地环境让我们看到）。</p>
<pre><code class="java">package com.shuyepl.threadState;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SleepTest &#123;
    public static void main(String[] args) &#123;
        // 创建Date对象
        Date nowTime = null;

        // SimpleDateFormat对象的创建
        SimpleDateFormat sdf = new SimpleDateFormat(&quot;HH:mm:ss&quot;);

        // 循环每一秒打印一次时间，打印时间间隔1秒
        for (int i = 0; i &lt; 10; i++) &#123;
            // 获取当前时间，格式化为字符串并打印
            nowTime = new Date();
            System.out.println(sdf.format(nowTime));
            // 休眠一秒
            try &#123;
                Thread.sleep(1000);
            &#125; catch (InterruptedException e) &#123;
                e.printStackTrace();
            &#125;
        &#125;
    &#125;
&#125;
</code></pre>
<h2 id="线程礼让"><a href="#线程礼让" class="headerlink" title="线程礼让"></a>线程礼让</h2><p>使用 yield 方法可以让当前的线程放弃使用的 CPU 时间片，返回到<strong>就绪状态</strong> ，再次等待 CPU 的调度，这种礼让的方式不一定会成功让其它线程执行，看 CPU 如何调度了。</p>
<pre><code class="java">package com.shuyepl.threadState;

public class YieldTest implements Runnable &#123;
    @Override
    public void run() &#123;
        for (int i = 0; i &lt; 100; i++) &#123;
            System.out.println(&quot;run ==&gt; &quot; + i);
            // 循环变量是10的倍数就礼让一次
            if (i % 10 == 0) &#123;
                Thread.yield();
            &#125;
        &#125;
    &#125;

    public static void main(String[] args) &#123;
        new Thread(new YieldTest()).start();
        for (int i = 0; i &lt; 100; i++) &#123;
            System.out.println(&quot;main ==&gt; &quot; + i);
        &#125;
    &#125;
&#125;
</code></pre>
<h2 id="线程插队"><a href="#线程插队" class="headerlink" title="线程插队"></a>线程插队</h2><p>join 方法可以让当前执行的线程暂停，而让调用 join 方法的线程执行（这个就是插队的线程），等待插队的线程执行结束之后，原来暂停的线程才继续执行。</p>
<pre><code class="java">package com.shuyepl.threadState;

public class JoinTest implements Runnable &#123;
    @Override
    public void run() &#123;
        for (int i = 0; i &lt; 100; i++) &#123;
            System.out.println(&quot;run ==&gt; &quot; + i);
        &#125;
    &#125;

    public static void main(String[] args) &#123;
        Thread thread = new Thread(new JoinTest());
        thread.start();

        for (int i = 0; i &lt; 100; i++) &#123;
            System.out.println(&quot;main ==&gt; &quot; + i);
            if (i == 50) &#123;
                try &#123;
                    // 插队，main线程阻塞，等待thread线程执行结束，main线程才执行
                    thread.join();
                &#125; catch (InterruptedException e) &#123;
                    e.printStackTrace();
                &#125;
            &#125;
        &#125;
    &#125;
&#125;
</code></pre>
<p>这个 join 方法，只会让执行这个代码的线程阻塞，比如下面这个代码，join 方法是在 main 里面执行的，只有 main 线程会阻塞，其他线程依旧正常执行</p>
<pre><code class="java">package com.shuyepl.threadState;

public class JoinTest implements Runnable &#123;
    @Override
    public void run() &#123;
        for (int i = 0; i &lt; 100; i++) &#123;
            System.out.println(Thread.currentThread() + &quot;run ==&gt; &quot; + i);
        &#125;
    &#125;

    public static void main(String[] args) &#123;
        Thread thread1 = new Thread(new JoinTest(), &quot;t1&quot;);
        thread1.start();

        Thread thread2 = new Thread(new JoinTest(), &quot;t2&quot;);
        thread2.start();


        for (int i = 0; i &lt; 100; i++) &#123;
            System.out.println(&quot;main ==&gt; &quot; + i);
            if (i == 50) &#123;
                try &#123;
                    // 插队，main线程阻塞，等待thread1线程执行结束，main线程才执行，thread2线程不受影响
                    thread1.join();
                &#125; catch (InterruptedException e) &#123;
                    e.printStackTrace();
                &#125;
            &#125;
        &#125;
    &#125;
&#125;
</code></pre>
<h1 id="线程状态检测"><a href="#线程状态检测" class="headerlink" title="线程状态检测"></a>线程状态检测</h1><p>通过 getState() 方法可以查看线程的当前状态，这些状态分别如下：</p>
<table>
<thead>
<tr>
<th align="center">线程状态</th>
<th align="center">解释</th>
</tr>
</thead>
<tbody><tr>
<td align="center">NEW</td>
<td align="center">尚未启动的线程在这个状态</td>
</tr>
<tr>
<td align="center">RUNNABLE</td>
<td align="center">线程在Java虚拟机中运行了</td>
</tr>
<tr>
<td align="center">BLOCKED</td>
<td align="center">被阻塞等待监视器锁定的线程处于这个状态</td>
</tr>
<tr>
<td align="center">WAITING</td>
<td align="center">等待另外一个线程执行特定动作的线程处于这个状态</td>
</tr>
<tr>
<td align="center">TIMED_WAITING</td>
<td align="center">正在等待另外一个线程执行动作到达指定等待时间的线程处于这个状态</td>
</tr>
<tr>
<td align="center">TERMINATED</td>
<td align="center">已经退出的线程所处的状态</td>
</tr>
</tbody></table>
<h1 id="线程优先级"><a href="#线程优先级" class="headerlink" title="线程优先级"></a>线程优先级</h1><p>使用 getPriority() 方法获取线程优先级，使用 setPriority(int priority) 设置线程优先级。</p>
<p>在 Thread 类中有几个常量，分别表示最小优先级，最大优先级和默认优先级</p>
<ul>
<li>Thread.MIN_PRIORITY = 1;</li>
<li>Thread.MAX_PRIORITY = 10;</li>
<li>Thread.NORM_PRIORITY = 5;</li>
</ul>
<p>Java 提供一个线程调度器监控程序中处于就绪状态的线程，线程调度器根据优先级决定调度哪个线程来执行，优先级的高低只是线程被 CPU 调度的概率高低而已，具体调度还得看 CPU 的决策。</p>
<h1 id="守护线程"><a href="#守护线程" class="headerlink" title="守护线程"></a>守护线程</h1><p>线程其实有两种，<strong>用户线程</strong> 和 <strong>用户线程</strong> ，虚拟机会确保用户线程执行完毕，但不会等待守护线程执行完毕，gc 垃圾回收就是一种守护线程。</p>
<p>调用线程的 setDaemon(true) 方法可将线程设置为守护线程，这个方法的默认参数是 false 的，也就是用户线程。</p>
<pre><code class="java">package com.shuyepl.threadState;

public class DaemonTest implements Runnable &#123;
    @Override
    public void run() &#123;
        for (int i = 0; i &lt; 10000000; i++) &#123;
            System.out.println(&quot;我是一个守护线程&quot;);
        &#125;
    &#125;

    public static void main(String[] args) &#123;
        Thread thread = new Thread(new DaemonTest());
        // 设置线程为守护线程
        thread.setDaemon(true);
        thread.start();

        for (int i = 0; i &lt; 10; i++) &#123;
            System.out.println(&quot;我是一个用户线程&quot;); // 运行结果是守护线程没有完整运行
        &#125;
    &#125;
&#125;
</code></pre>
<h1 id="线程同步机制"><a href="#线程同步机制" class="headerlink" title="线程同步机制"></a>线程同步机制</h1><p>多个线程同时操作同一个对象的时候，可能会发生错误，例如：我和我的朋友分享了我的银行卡账号和密码，卡里有100块钱，如果我们同时一个在手机操作取钱，一个在 ATM 机操作取钱，假如我们这两个取钱的线程同步启动，并且所有执行的代码都同步运行（很理想），那我们看到的银行卡的余额就都是100块对吧，如果这个时候我在 ATM 取100块，我的朋友在手机上取一百块，那因为我们从数据库中查看到的数据都是有100块钱余额，那就都取成功了，余额变成0了，结果是卡里的100块变成200块了，这个很魔幻对不对，算是一个很致命的错误了，银行不可能让人这样子取钱的，不然早破产了，那这里面就涉及到线程控制了。</p>
<p>线程控制简单点说就是大家修改数据库数据的时候需要排队，一个一个来，不能一起来。用上面的例子来说就是，当我们两个人取钱的请求到达服务器之后，必须排队去操作我卡里的余额，只有前面一个人的业务完成之后，修改完数据库中的余额后，后面的人才能进入数据库中去操作，所以，因为我已经先取完100块钱了，数据库中的余额变为0了，我的朋友进来想取100块钱的话，就余额不足了，取款失败。</p>
<h2 id="synchronized"><a href="#synchronized" class="headerlink" title="synchronized"></a>synchronized</h2><p>控制线程的方式可以是使用 synchronized 关键字给代码块加锁。</p>
<hr>
<p>文章引用资料来自：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://www.runoob.com/">菜鸟教程</a></li>
<li><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1V4411p7EF/?p=28&spm_id_from=333.880.my_history.page.click&vd_source=3b9bc9314c9590a1d18a84ef490fb982">多线程28：总结_哔哩哔哩_bilibili</a></li>
</ul>

      
       <hr><span style="font-style: italic;color: gray;"> 转载请注明来源，欢迎对文章中的引用来源进行考证，欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论，也可以邮件至 3548768596@qq.com </span>
    </div>
</article>







    




    </div>
    <div class="copyright">
        <p class="footer-entry">
    ©2017 ShuYePL
</p>
<p class="footer-entry">Built with <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/yelog/hexo-theme-3-hexo" target="_blank">3-hexo</a> theme</p>

    </div>
    <div class="full-toc">
        <button class="full" data-title="切换全屏"><span class="min "></span></button>
<a class="" id="rocket" ></a>

    </div>
</div>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"live2d-widget-model-hijiki"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"rect":"opacity:0.7","log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
<script src="/js/jquery.pjax.js?v=1.1.0" ></script>

<script src="/js/script.js?v=1.1.0" ></script>
<script>
    var img_resize = 'default';
    function initArticle() {
        /*渲染对应的表格样式*/
        
            $("#post .pjax table").addClass("green_title");
        

        /*渲染打赏样式*/
        

        /*高亮代码块行号*/
        

        /*访问数量*/
        
        $.getScript("//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js");
        

        /*代码高亮，行号对齐*/
        $('.pre-numbering').css('line-height',$('.has-numbering').css('line-height'));

        
        
    }

    /*打赏页面隐藏与展示*/
    

</script>

<!--加入行号的高亮代码块样式-->

<!--自定义样式设置-->
<style>
    
    
    .nav {
        width: 542px;
    }
    .nav.fullscreen {
        margin-left: -542px;
    }
    .nav-left {
        width: 120px;
    }
    
    
    @media screen and (max-width: 1468px) {
        .nav {
            width: 492px;
        }
        .nav.fullscreen {
            margin-left: -492px;
        }
        .nav-left {
            width: 100px;
        }
    }
    
    
    @media screen and (max-width: 1024px) {
        .nav {
            width: 492px;
            margin-left: -492px
        }
        .nav.fullscreen {
            margin-left: 0;
        }
    }
    
    @media screen and (max-width: 426px) {
        .nav {
            width: 100%;
        }
        .nav-left {
            width: 100%;
        }
    }
    
    
    .nav-right .title-list nav a .post-title, .nav-right .title-list #local-search-result a .post-title {
        color: #383636;
    }
    
    
    .nav-right .title-list nav a .post-date, .nav-right .title-list #local-search-result a .post-date {
        color: #5e5e5f;
    }
    
    
    .nav-right nav a.hover, #local-search-result a.hover{
        background-color: #2e2f2f;
    }
    
    
    .nav-right nav a.hover > span{
        color: #ffffff !important;
    }
    

    /*列表样式*/
    

    /* 背景图样式 */
    
    #post {
        background: url(https://i.loli.net/2019/07/22/5d3521411f3f169375.png);
    }
    
    
    #post .index {
        background: url(https://i.loli.net/2019/07/22/5d3521411f3f169375.png);
    }
    


    /*引用块样式*/
    

    /*文章列表背景图*/
    
    .nav-right:before {
        content: ' ';
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: 0.4;
        background: url("https://i.loli.net/2019/07/22/5d3521411f3f169375.png");
        background-repeat: no-repeat;
        background-position: 50% 0;
        -ms-background-size: cover;
        -o-background-size: cover;
        -moz-background-size: cover;
        -webkit-background-size: cover;
        background-size: cover;
    }
    

    
</style>







</html>
